{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Learning Content\n",
    "\n",
    "Decision Trees Using Information Theory:  \n",
    "\n",
    "Entropy:  \n",
    "Formula:($H(D) = - \\sum_{i=1}^{k} p_i \\log_2(p_i)$)  \n",
    "($p_i$) is the probability of class ($i$) in dataset ($D$)\n",
    "\n",
    "Information Gain:  \n",
    "Formula:($IG(D, A) = H(D) - \\sum_{v \\in \\text{Values}(A)} \\frac{|D_v|}{|D|} H(D_v)$)  \n",
    "($D_v$) is the subste of ($D$) for which attribute ($A$) has value ($v$)  \n",
    "\n",
    "Random Forests and Ensemble Methods:  \n",
    "Random Forests  \n",
    "Ensemble Methods  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Decision Tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Decision Tree Accuracy: 0.895\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "sh: 1: xdg-open: not found\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "# 生成示例数据\n",
    "np.random.seed(42)\n",
    "X=2*np.random.rand(1000,2)\n",
    "y=(X[:,0]+X[:,1]>2).astype(int)\n",
    "\n",
    "# 划分训练集和测试集\n",
    "X_train, X_test, y_train, y_test=train_test_split(X,y,test_size=0.2,random_state=42)\n",
    "\n",
    "# 创建决策树模型\n",
    "tree_clf=DecisionTreeClassifier(criterion='entropy',max_depth=3,random_state=42)\n",
    "\n",
    "# 训练模型\n",
    "tree_clf.fit(X_train,y_train)\n",
    "\n",
    "# 预测\n",
    "y_pred=tree_clf.predict(X_test)\n",
    "\n",
    "# 计算准确率\n",
    "accuracy=accuracy_score(y_test,y_pred)\n",
    "print(f\"Decision Tree Accuracy: {accuracy}\")\n",
    "\n",
    "\n",
    "# 可视化决策树\n",
    "\n",
    "from sklearn.tree import export_graphviz\n",
    "import graphviz\n",
    "\n",
    "dot_data=export_graphviz(\n",
    "    tree_clf,\n",
    "    feature_names=['Feature 1',' Freature 2'],\n",
    "    class_names=['Class 0','Class 1'],\n",
    "    filled=True,\n",
    "    rounded=True,\n",
    "    special_characters=True\n",
    ")\n",
    "\n",
    "graph=graphviz.Source(dot_data)\n",
    "graph.render('decision_tree')\n",
    "\n",
    "\n",
    "import os\n",
    "import platform\n",
    "\n",
    "\n",
    "def open_pdf(file_path):\n",
    "    if platform.system()=='Darwin':\n",
    "        os.system(f'open {file_path}')\n",
    "    elif platform.system()=='Windows':\n",
    "        os.system(f'start {file_path}')\n",
    "        os.startfile(file_path)\n",
    "    else:\n",
    "        os.system(f'xdg-open {file_path}')\n",
    "open_pdf('decision_tree.pdf')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm-learning",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
